<template>
  <el-dialog title="文件列表" :visible.sync="visible" :close-on-click-modal="false">
    <el-table :data="dataList">
      <el-table-column property="fileName" label="文件名称" width="150"></el-table-column>
      <el-table-column property="fileUrl" label="文件上传路径"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" style="font-size:14px" @click="deleteHandle(scope.row.fileId)">删除</el-button>
          <el-button type="text" size="small" style="font-size:14px" @click="restoreHandle(scope.row.fileId)">收藏</el-button>
          <a :href="scope.row.fileUrl" style="display:inherit;font-size:14px">下载</a>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataList: []
    };
  },
  methods: {
    init(id) {
      this.visible = true;
      this.$http({
        url: this.$http.adornUrl(`/generator/file/fileListDir/${id}`),
        method: "get",
        data: this.$http.adornParams(),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.fileList;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    deleteHandle (id) {
        this.$confirm(`确定对文件进行删除吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/generator/file/delete/${id}`),
            method: 'get',
            data: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      restoreHandle(id) {
        this.$confirm(`确定收藏该文件吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/generator/file/restore/${id}`),
            method: 'get',
            data: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
  },
};
</script>
